<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>柠檬水找零</title>
</head>

<body>
  <script>
    function array(bills) {
      let ten = 0
      let five = 0

      for (let i = 0; i < bills.length; i++) {
        const cur = bills[i]

        if (cur === 5) five++;
        else if (cur === 10) ten++, five--;
        else if (ten > 0) ten--, five--
        else five -= 3

        if (five < 0) return false
      }

      return true
    }
    console.log(array([5, 5, 10, 10, 20]))
  </script>
</body>

</html>